package Hash;

import java.util.*;
import java.util.concurrent.BlockingQueue;

/**
 * @Author: Darven
 * @CreateTime: 2025-03-30  14:07
 * @Description: TODO
 */
public class P49 {
    public static void main(String[] args) {
        byte[] bytes={'e','a','t'};
        Arrays.sort(bytes);
        String string = Arrays.toString(bytes);
        System.out.println(string);
        String value = new String(bytes);
        System.out.println(value);

        byte[] bytes1={'t','a','e'};
        Arrays.sort(bytes1);
        String s = new String(bytes1);
        System.out.println(s);

        Map<String,Integer> map=new HashMap<>();
        map.put(value, 1);
        boolean b = map.containsKey(s);
        System.out.println(b);


    }
}

class P49Demo{
    public List<List<String>> groupAnagrams(String[] strs) {
        //通过排好序的key
        Map<String,Integer> map=new HashMap<>();

        List<List<String>> ans=new ArrayList<>();

        for(String s:strs){
            byte[] bytes = s.getBytes();
            Arrays.sort(bytes);
            String key = new String(bytes);
            List<String> stringList=null;
            if(map.containsKey(key)){
                Integer index = map.get(key);
                stringList = ans.get(index);
                stringList.add(s);
                continue;
            }else {
                stringList=new ArrayList<>();
                stringList.add(s);
                ans.add(stringList);
            }
            int size=ans.size()==1?0: ans.size()-1;
            map.put(key,size);
        }


        return ans;
    }

    public List<List<String>> groupAnagrams2(String[] strs) {
        //通过排好序的key
        Map<String,List<String>> map=new HashMap<>();
        for(String s:strs){
            byte[] bytes = s.getBytes();
            Arrays.sort(bytes);
            String key = new String(bytes);
            List<String> stringList=null;
            if(map.containsKey(key)){
                stringList = map.get(key);
                stringList.add(s);
                continue;
            }
            stringList=new ArrayList<>();
            stringList.add(s);
            map.put(key, stringList);
        }
        List<List<String>> list = map.values().stream().toList();

        return list;
    }

    public List<List<String>> groupAnagrams3(String[] strs) {
        //通过排好序的key
        Map<String,List<String>> map=new HashMap<>();

        for(String s:strs){
           int[] count=new int[26];
           for(int i=0;i<s.length();i++){
               count[s.charAt(i)-'a']++;
           }
            StringBuffer sb = new StringBuffer();
           for(int i=0;i<26;i++){
               if(count[i]!=0){
                   sb.append((char)i+'a');
                   sb.append(count[i]);
               }
           }
            String key = sb.toString();
            List<String> list = map.getOrDefault(key, new ArrayList<>());
            list.add(s);
            map.put(key, list);

        }

        return  map.values().stream().toList();

    }
}
